package com.lwj.widget.viewpagerindicator_demo.slice;

import com.lwj.widget.viewpagerindicator.ResourceTable;
import com.lwj.widget.viewpagerindicator.ViewPagerIndicator;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.PageSlider;
import ohos.agp.components.PageSliderProvider;
import ohos.agp.components.Text;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;

/**
 * 轮播页面
 *
 * @author name
 * @since 2021-03-20
 */
public class CarouseAbilitySlice extends AbilitySlice {
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0, "CarouseAbilitySlice");
    private static final int TEXT_WIDTH = 100;
    private static final int TEXT_HEIGHT = 100;
    private static final int TEXT_SIZE = 40;
    private PageSlider mPageSlider;
    private ViewPagerIndicator mIndicatorDefault;
    private ViewPagerIndicator mIndicatorCircle;
    private ViewPagerIndicator mIndicatorLine;
    private ViewPagerIndicator mIndicatorCircleLine;
    private ViewPagerIndicator mIndicatorBazier;
    private ViewPagerIndicator mIndicatorSpring;
    private ViewPagerIndicator mIndicatorProgress;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_carouse);
        mPageSlider = (PageSlider) findComponentById(ResourceTable.Id_page_slider);
        ArrayList<String> strings = new ArrayList<>();
        strings.add("NO.1");
        strings.add("NO.2");
        strings.add("NO.3");
        strings.add("NO.4");
        final int num = strings.size();
        FractionAdapter mFractionAdapterFraction = new FractionAdapter(strings, this);
        if (mPageSlider == null) {
            HiLog.error(LABEL, "mPageSlider is null");
        }
        mPageSlider.setCircularModeEnabled(true);
        mPageSlider.setProvider(mFractionAdapterFraction);
        mPageSlider.setCurrentPage(0,false);
        mIndicatorDefault = (ViewPagerIndicator) findComponentById(ResourceTable.Id_default_indicator);
        mIndicatorCircle = (ViewPagerIndicator) findComponentById(ResourceTable.Id_circle_indicator);
        mIndicatorLine = (ViewPagerIndicator) findComponentById(ResourceTable.Id_line_indicator);
        mIndicatorCircleLine = (ViewPagerIndicator) findComponentById(ResourceTable.Id_circle_line_indicator);
        mIndicatorBazier = (ViewPagerIndicator) findComponentById(ResourceTable.Id_bezier_indicator);
        mIndicatorSpring = (ViewPagerIndicator) findComponentById(ResourceTable.Id_spring_indicator);
        mIndicatorProgress = (ViewPagerIndicator) findComponentById(ResourceTable.Id_process_indicator);
        mIndicatorDefault.setViewPager(mPageSlider, num);
        mIndicatorCircle.setViewPager(mPageSlider, num);
        mIndicatorLine.setViewPager(mPageSlider, num);
        mIndicatorCircleLine.setViewPager(mPageSlider, num);
        mIndicatorBazier.setViewPager(mPageSlider, num);
        mIndicatorSpring.setViewPager(mPageSlider, num);
        mIndicatorProgress.setViewPager(mPageSlider, num);

        // 点击跳转到 not_carous
        Button button = (Button) findComponentById(ResourceTable.Id_to_not_carous);
        button.setMultipleLine(true);
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                present(new NotCarouselAbilitySlice(), new Intent());
            }
        });
    }

    /**
     * FractionAdapterFraction
     *
     * @author name
     * @since 2021-03-20
     */
    private static class FractionAdapter extends PageSliderProvider {
        private Context mContext;
        private ArrayList<String> mStrings;

        /**
         * 构造方法
         *
         * @param strings
         * @param context
         */
        FractionAdapter(ArrayList<String> strings, Context context) {
            super();
            mStrings = strings;
            mContext = context;
        }

        /**
         * 获取数量
         *
         * @return num 数目
         */
        @Override
        public int getCount() {
            return mStrings.size();
        }

        /**
         * 创建页面时
         *
         * @param componentContainer 组件容器
         * @param i
         * @return Object
         */
        @Override
        public Object createPageInContainer(ComponentContainer componentContainer, int i) {
            Text notCarouseText = new Text(mContext);
            notCarouseText.setWidth(TEXT_WIDTH);
            notCarouseText.setHeight(TEXT_HEIGHT);
            notCarouseText.setText(mStrings.get(i));
            notCarouseText.setTextSize(TEXT_SIZE);
            DirectionalLayout directionalLayout = new DirectionalLayout(mContext);
            directionalLayout.setOrientation(Component.VERTICAL);
            directionalLayout.setLayoutConfig(new ComponentContainer.LayoutConfig(MATCH_PARENT, MATCH_PARENT));
            directionalLayout.addComponent(notCarouseText);
            componentContainer.addComponent(directionalLayout);

            return directionalLayout;
        }

        /**
         * 容器销毁页面时
         *
         * @param componentContainer 组件容器
         * @param i
         * @param o
         */
        @Override
        public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
            componentContainer.removeComponent((Component) o);
        }

        /**
         * 页面匹配
         *
         * @param component 组件
         * @param obj
         * @return boolean
         */
        @Override
        public boolean isPageMatchToObject(Component component, Object obj) {
            return component == obj;
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
